package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Book;
import com.example.demo.entity.User;
import com.example.demo.mapper.BookMapper;
import com.example.demo.service.BookService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/book")
public class BookController {

    @Resource
    BookService bookService;
    @Resource
    BookMapper bookMapper;

    @PostMapping
    public Result<?> save(@RequestBody Book book){
        bookService.insert(book);
        return Result.success();
    }

    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "10") Integer pageSize,@RequestParam(defaultValue = "") String searchStr){
        /**
         * searchStr 空字符串的处理 导入三方库 hutool
         */
        LambdaQueryWrapper<Book> wrapper = Wrappers.<Book>lambdaQuery();
        if (StringUtils.isNotBlank(searchStr)){//searchStr不为空，再设置为模糊查询
            wrapper.like(Book::getName, searchStr);
        }
        Page<Book> bookPage = bookMapper.selectPage(new Page<>(pageNum,pageSize),wrapper);
        //拿到数据后渲染vue页面，传给tableData
        return Result.success(bookPage);
    }

    @DeleteMapping("/{id}")
    public Result<?> update(@PathVariable Long id){

        bookMapper.deleteById(id);
        return Result.success();
    }
}
